package com.miyake.client.datamanage.plugin;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DataService;
import com.miyake.client.tools.OkCancelDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.SmallButton;

public abstract class PluginDialog extends OkCancelDialog {
	private String selectedName;
	private String selectedKey;
	private String selectedUrl;
	private Grid grid;
	
	abstract protected DialogBox getAddDialog();
	abstract protected void getPluginList();
	
	@Override
	protected Widget getContent() {
		setHTML("Plugin Market");
		VerticalPanel panel = new VerticalPanel();
		panel.add(new SmallButton("Add", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				new ShowDialog() {

					@Override
					protected void onOK(String text) {
						getPluginList();
					}

					@Override
					protected DialogBox getDialogBox() {
						return getAddDialog();
					}
					
				};
			}
			
		}));
		
		grid = new Grid();
		panel.add(grid);
		getPluginList();
		
		return panel;
	}

	public String getSelectedName() {
		return selectedName;
	}

	public void setSelectedName(String selectedName) {
		this.selectedName = selectedName;
	}

	public String getSelectedKey() {
		return selectedKey;
	}

	public void setSelectedKey(String selectedKey) {
		this.selectedKey = selectedKey;
	}

	public String getSelectedUrl() {
		return selectedUrl;
	}

	public void setSelectedUrl(String selectedUrl) {
		this.selectedUrl = selectedUrl;
	}
	
	private void deletePlugin(String key) {
		DataService.Util.getInstance().deletePlugin(key, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {
				getPluginList();
			}
		});
	}

	protected void showList(List<PluginProperty> result) {
		grid.resize(result.size()+1, 8);
		grid.setBorderWidth(1);
		
		grid.setWidget(0, 0, new HTML("<b>Service Name</b>"));
		grid.setWidget(0, 1, new HTML("<b>Description</b>"));
		grid.setWidget(0, 2, new HTML("<b>Provider</b>"));
		grid.setWidget(0, 3, new HTML("<b>Version</b>"));
		grid.setWidget(0, 4, new HTML("<b>License</b>"));
		grid.setWidget(0, 5, new HTML("<b>Web</b>"));
		//grid.setWidget(0, 5, new HTML("Name"));
		
		int row = 1;
		for (final PluginProperty p : result) {
			int col = 0;
			grid.setText(row, col++, p.getName());
			grid.setText(row, col++, p.getDescription());
			grid.setText(row, col++, p.getProvider());
			grid.setText(row, col++, p.getVersion());
			grid.setText(row, col++, p.getLicense());
			grid.setWidget(row, col++, new HTML("<a href=\"" + p.getHelpUrl()+ "\">Link</a>"));
			grid.setWidget(row, col++, new Button("Get", new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					selectedName = p.getName();
					selectedKey = p.getKey();
					selectedUrl = p.getUrl();
					hide(false);
				}
			}));
			
			grid.setWidget(row, col++, new Button("Delete", new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					deletePlugin(p.getKey());
				}
			}));
			row++;
		}
	}
	
}
